<template>
  <div class="app-container">
    <span>跳转中...</span>

  </div>
</template>

<script>
    import { loginByWechat,loginByQQ,loginByAlipay } from '../api/login';
    import { getUrlParam } from '../utils/index';
    import { userLoginLog } from "@/api/log";
    export default{
        data(){
            return{
                authCode:'',
                loginLog:{ //用户登录日志
                    systemId:3,
                    environmentId:1,
                    serviceId:13,
                    loginType:1,
                    username:'',
                }
            }
        },
        methods:{
            // 第三方登录
            getThirdLogin(){
                let bind = getUrlParam('isbind');
                let type = getUrlParam('type');
                this.authCode = getUrlParam('code');
                let params = {
                    authCode:this.authCode
                }
                if(type == 0){
                    loginByWechat(params).then(res => {
                        console.log(res,'3333333');
                        if(res.success){
                            this.$store.dispatch('SetInToken', res.result).then(()=>{
                                if (window.opener) {
                                    window.opener.location.replace(`${location.protocol}//${location.host}/#/`);
                                    window.close();
                                } else {
                                    this.$router.push({ path: '/' });
                                }
                                this.loginLog.loginType = 3;
                                this.loginLog.username = res.result.username;
                                console.log(this.loginLog,'this.loginLogthis.loginLog');
                                userLoginLog(this.loginLog).then(res=>{
                                    // console.log(res,'手机登录');
                                })
                            })
                        }else{
                            // 登录失败（未绑定）
                            if (res.result.status == 40003) {
                                if (bind) {
                                    // 已登录 绑定微信
                                    console.log('22222222');
                                }else{
                                    console.log('22222222333');
                                    // 首页扫码登陆未绑定->绑定
                                    if (window.opener) {
                                        console.log(window.opener,'微信');
                                        window.opener.location.replace(
                                            `${location.protocol}//${location.host}/#/login?type=0&openID=${res.result.openId}&unionID=${res.result.unionId}&bind=true`
                                        )
                                        window.opener.location.reload();
                                        window.close();
                                    } else {
                                        this.$router.push({ path: '/' });
                                    }
                                }
                            }else{
                                if (window.opener) {
                                    window.opener.location.replace(
                                        `${location.protocol}//${location.host}/#/login?error=${
                                            res.message || '登陆错误'
                                        }`
                                    );
                                    window.opener.location.reload();
                                    window.opener.close();
                                }
                            }
                        }
                    });
                }else if(type == 1){
                    loginByQQ(params).then(res => {
                        console.log(res,'qq');
                        if(res.success){
                            this.$store.dispatch('SetInToken', res.result).then(()=>{
                                if (window.opener) {
                                    window.opener.location.replace(`${location.protocol}//${location.host}/#/`);
                                    window.close();
                                } else {
                                    this.$router.push({ path: '/' });
                                }
                                this.loginLog.loginType = 4;
                                this.loginLog.username = res.result.username;
                                // console.log(this.loginLog,'this.loginLogthis.loginLog');
                                userLoginLog(this.loginLog).then(res=>{
                                    console.log(res,'手机登录');
                                })
                            })
                        }else{
                            // 登录失败（未绑定）
                            if (res.result.status == 40003) {
                                if (bind) {
                                    // 已登录 绑定微信
                                    // console.log('22222222');
                                }else{
                                    // 首页扫码登陆未绑定->绑定
                                    if (window.opener) {
                                        console.log(window.opener);
                                        window.opener.location.replace(
                                            `${location.protocol}//${location.host}/#/login?type=1&openID=${res.result.openId}&bind=true`
                                        );
                                        window.opener.location.reload();
                                        window.close();
                                    } else {
                                        this.$router.push({ path: '/' });
                                    }
                                }
                            }else{
                                if (window.opener) {
                                    window.opener.location.replace(
                                        `${location.protocol}//${location.host}/#/login?error=${
                                            res.message || '登陆错误'
                                        }`
                                    );
                                    window.opener.location.reload();
                                    window.close();
                                }
                            }

                        }
                    })
                }else if(type == 2){
                    let code = getUrlParam('auth_code');
                    let params = {
                        authCode:code
                    }
                    loginByAlipay(params).then(res => {
                        console.log(res,'支付宝');
                        if(res.success){
                            this.$store.dispatch('SetInToken', res.result).then(()=>{
                                if (window.opener) {
                                    window.opener.location.replace(`${location.protocol}//${location.host}/#/`);
                                    window.close();
                                } else {
                                    this.$router.push({ path: '/' });
                                }
                                this.loginLog.loginType = 5;
                                this.loginLog.username = res.result.username;
                                userLoginLog(this.loginLog).then(res=>{
                                    console.log(res,'手机登录');
                                })
                            })
                        }else{
                            // 登录失败（未绑定）
                            if (res.result.status == 40003) {
                                if (bind) {
                                    // 已登录 绑定微信
                                    // console.log('22222222');
                                }else{
                                    // 首页扫码登陆未绑定->绑定
                                    if (window.opener) {
                                        console.log(window.opener);
                                        window.opener.location.replace(
                                            `${location.protocol}//${location.host}/#/login?type=2&openID=${res.result.openId}&bind=true`
                                        );
                                        window.opener.location.reload();
                                        window.close();
                                    } else {
                                        this.$router.push({ path: '/' });
                                    }
                                }
                            }else{
                                // debugger;;
                                if (window.opener) {
                                    window.opener.location.replace(
                                        `${location.protocol}//${location.host}/#/login?error=${
                                            res.message || '登陆错误'
                                        }`
                                    );
                                    window.opener.location.reload();
                                    window.close();
                                }
                            }

                        }
                    })
                }
            },
        },
        created(){
            let site = process.env.VUE_APP_BASE_URL.split('/')[2]
            if(site == 'out.bj.51db.com:36101'){
            this.loginLog.environmentId = 1
            }else if(site == 'out.bj.51db.com:36201'){
            this.loginLog.environmentId = 2
            }else if(site == 'srm.b1b.cn:36301'){
            this.loginLog.environmentId = 4
            }
            this.getThirdLogin();
        },

    }
</script>
<style lang="scss" scoped>

    
</style>